package action.FrontAction;

import com.opensymphony.xwork2.ActionSupport;
import common.Pagination;
import entity.CroBills;
import entity.CroCustomer;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroBillsService;
import service.CroCustomerService;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * Created by colin on 2017/5/21.
 */
public class WalletAction extends ActionSupport {

    @Autowired
    private CroCustomerService croCustomerService;

    @Autowired
    private CroBillsService croBillsService;

    private List<CroBills> bills;
    private CroCustomer mine;
    private String billId;
    private double amount;
    private String jsonResult;
    private int pageIndex;
    private int pageCount;
    private int pageSize = 10;

    /**
     * 所有消费记录
     * @return
     */
    public String wallet(){
        ServletContext context = ServletActionContext.getServletContext();
        CroCustomer customer = (CroCustomer) context.getAttribute("customer");
        mine = croCustomerService.FindCustomerById(customer.getId());
        Pagination<CroBills> page = null;
        if (pageIndex == 0){
            //前台没有传值的情况
            page = croBillsService.listPageByCus(customer,1,pageSize);
        } else {
            page = croBillsService.listPageByCus(customer,pageIndex,pageSize);
        }
        bills = page.getItems();
        pageIndex = (int) page.getCurIndex();
        pageCount = (int) page.getRowsCount();
        return SUCCESS;
    }

    /**
     * 充值
     * @return
     */
    public String topUp(){
        ServletContext context = ServletActionContext.getServletContext();
        CroCustomer customer = (CroCustomer) context.getAttribute("customer");
        double balence = customer.getBalance();
        balence = balence+amount;
        customer.setBalance(balence);
        CroBills bill = new CroBills();
        bill.setCroCustomer(customer);
        bill.setBilltype("0");
        bill.setConsumption(amount);
        if (croBillsService.addBills(bill) && croCustomerService.updateCustomer(customer)){
            jsonResult = "{\"status\":1,\"msg\":\"充值成功!\"}";
        }else {
            jsonResult = "{\"status\":0,\"msg\":\"充值失败!\"}";
        }
        return SUCCESS;
    }

    /**
     * 删除消费记录
     * @return
     */
    public String delBill(){
        CroBills bill = croBillsService.findById(billId);
        bill.setDel_flag("1");
        if (croBillsService.updateBills(bill)){
            jsonResult = "{\"status\":1,\"msg\":\"删除成功!\"}";
        }else {
            jsonResult = "{\"status\":0,\"msg\":\"删除失败!\"}";
        }
        return SUCCESS;
    }

    public List<CroBills> getBills() {
        return bills;
    }

    public void setBills(List<CroBills> bills) {
        this.bills = bills;
    }

    public String getBillId() {
        return billId;
    }

    public void setBillId(String billId) {
        this.billId = billId;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public String getJsonResult() {
        return jsonResult;
    }

    public void setJsonResult(String jsonResult) {
        this.jsonResult = jsonResult;
    }

    public CroCustomer getMine() {
        return mine;
    }

    public void setMine(CroCustomer mine) {
        this.mine = mine;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }
}
